package com.mofeng.myshop.until;

import lombok.Data;
import lombok.Getter;

@Data
public class R {
    private boolean success;
    private int code;
    private String message;
    private Object data;

    public static R SUCCESS() {
        return new R(ResponseState.SUCCESS);
    }

    public static R SUCCESS(String message) {
        R responseResult = new R(ResponseState.SUCCESS);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static R SUCCESS(String message, Object data) {
        R responseResult = SUCCESS(message);
        responseResult.setData(data);
        return responseResult;
    }

    public static R FAILED() {
        return new R(ResponseState.FAILED);
    }

    public static R FAILED(String message) {
        R responseResult = new R(ResponseState.FAILED);
        responseResult.setMessage(message);
        return responseResult;
    }


    public static R TIME_OUT() {
        return new R(ResponseState.TIME_OUT);
    }

    public static R PERMISSION_DENIED() {
        return new R(ResponseState.PERMISSION_DENIED);
    }

    public static R ERROR_403() {
        return new R(ResponseState.ERROR_403);
    }

    public static R ERROR_404() {
        return new R(ResponseState.ERROR_404);
    }

    public static R ERROR_504() {
        return new R(ResponseState.ERROR_504);
    }

    public static R ERROR_505() {
        return new R(ResponseState.ERROR_505);
    }

    private R(ResponseState responseState) {
        this.success = responseState.isSuccess();
        this.code = responseState.getCode();
        this.message = responseState.getMessage();
    }

    public R setData(Object data) {
        this.data = data;
        return this;
    }

    @Getter
    private enum ResponseState {
        /*
         * 1000系列表示成功
         * 2000系列表示失败
         * */
        SUCCESS(true, 1000, ""),
        FAILED(false, 2000, ""),

        //错误码系列
        ERROR_403(false, 403, "权限不足，禁止访问"),
        ERROR_404(false, 404, "页面丢失！"),
        ERROR_504(false, 504, "系统繁忙，请稍后重试！"),
        ERROR_505(false, 505, "请求错误，请检查所提交数据"),
        TIME_OUT(false, 600, "当前日期已超过试用时间，请联系作者续期"),
        PERMISSION_DENIED(false, 601, "权限不足，请联系管理员升级权限");

        private final boolean success;
        private final int code;
        private final String message;

        ResponseState(boolean success, int code, String message) {
            this.success = success;
            this.code = code;
            this.message = message;
        }
    }
}
